package com.axc.gallery2.menu;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.axc.gallery2.R;
import com.axc.gallery2.Utils;
import com.axc.gallery2.dbhelper.AlbumInfo;
import com.axc.gallery2.dbhelper.ImageInfo;

public class RootMenu extends LinearLayout {
	private Context context;
	private RootAdapter rootAdapter;
	private BaseAdapter adapter;
	private ImageButton imgBtnAll;
	private ImageButton imgBtnLocal;
	private ImageButton imgBtnNetWork;
	private ImageButton imgBtnEdit;
	private ImageButton imgBtnCamera;
	private ImageButton imgBtnMore;
	HorizontalScrollView horizontalScrollView;
	View view;

	public RootMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public RootMenu(Context context) {
		super(context);
		this.context = context;
	}

	public void initView() {
		rootAdapter = new RootAdapter(getContext());
		this.adapter = rootAdapter;
		view = inflate(getContext(), R.layout.menu, null);
		horizontalScrollView = (HorizontalScrollView) view
				.findViewById(R.id.horizontalScrollView);
		imgBtnAll = getButton(R.id.imgBtnAll);
		imgBtnAll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (horizontalScrollView.getVisibility() == View.VISIBLE) {
					horizontalScrollView.setVisibility(View.INVISIBLE);
					view
							.setLayoutParams(new LayoutParams(
									LayoutParams.FILL_PARENT,
									view.getHeight() / 12 * 5));// 十二分之五的高度
				} else {
					horizontalScrollView.setVisibility(View.VISIBLE);
					view
							.setLayoutParams(new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.FILL_PARENT));
				}
			}
		});
		imgBtnLocal = getButton(R.id.imgBtnLocal);
		imgBtnLocal.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (horizontalScrollView.getVisibility() == View.INVISIBLE) {
					horizontalScrollView.setVisibility(View.VISIBLE);
					view
							.setLayoutParams(new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.FILL_PARENT));
				}
				LinearLayout tempLayout = (LinearLayout) horizontalScrollView
						.findViewById(R.id.menu_location);
				horizontalScrollView.smoothScrollTo(0, 0);

				tempLayout.setFocusableInTouchMode(true);
				tempLayout.requestFocus();
			}
		});
		imgBtnNetWork = getButton(R.id.imgBtnNetWork);
		imgBtnNetWork.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (horizontalScrollView.getVisibility() == View.INVISIBLE) {
					horizontalScrollView.setVisibility(View.VISIBLE);
					view
							.setLayoutParams(new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.FILL_PARENT));
				}

				LinearLayout tempLayout = (LinearLayout) horizontalScrollView
						.findViewById(R.id.menu_net);
				// 计算滚动值
				int off = calcOff(1);

				if (off >= 0) {
					horizontalScrollView.smoothScrollTo(off, 0);
				}

				tempLayout.setFocusableInTouchMode(true);
				tempLayout.requestFocus();
			}
		});
		imgBtnCamera = getButton(R.id.imgBtnCamera);
		imgBtnCamera.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (horizontalScrollView.getVisibility() == View.INVISIBLE) {
					horizontalScrollView.setVisibility(View.VISIBLE);
					view
							.setLayoutParams(new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.FILL_PARENT));
				}

				LinearLayout tempLayout = (LinearLayout) horizontalScrollView
						.findViewById(R.id.menu_camera);
				// 计算滚动值
				int off = calcOff(2);

				if (off >= 0) {
					horizontalScrollView.smoothScrollTo(off, 0);
				}
				tempLayout.setFocusableInTouchMode(true);
				tempLayout.requestFocus();
			}
		});
		imgBtnEdit = getButton(R.id.imgBtnEdit);
		imgBtnEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (horizontalScrollView.getVisibility() == View.INVISIBLE) {
					horizontalScrollView.setVisibility(View.VISIBLE);
					view
							.setLayoutParams(new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.FILL_PARENT));
				}

				LinearLayout tempLayout = (LinearLayout) horizontalScrollView
						.findViewById(R.id.menu_edit);
				// 计算滚动值
				int off = calcOff(3);

				if (off >= 0) {
					horizontalScrollView.smoothScrollTo(off, 0);
				}

				tempLayout.setFocusableInTouchMode(true);
				tempLayout.requestFocus();
			}
		});
		imgBtnMore = getButton(R.id.imgBtnMore);
		imgBtnMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (horizontalScrollView.getVisibility() == View.INVISIBLE) {
					horizontalScrollView.setVisibility(View.VISIBLE);
					view
							.setLayoutParams(new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.FILL_PARENT));
				}
				LinearLayout tempLayout = (LinearLayout) horizontalScrollView
						.findViewById(R.id.menu_more);
				// 计算滚动值
				int off = calcOff(4);
				if (off >= 0) {
					horizontalScrollView.smoothScrollTo(off, 0);
				}

				tempLayout.setFocusableInTouchMode(true);
				tempLayout.requestFocus();
			}
		});

		// 设置每个功能菜单对应的子菜单
		LinearLayout line = new LinearLayout(getContext());
		line.setOrientation(LinearLayout.HORIZONTAL);

		int count = adapter.getCount();
		for (int i = 0; i < count; i++) {
			View v = adapter.getView(i, null, null);
			line.addView(v, i);
		}
		horizontalScrollView.addView(line);
		this.addView(view);
	}

	private ImageButton getButton(int id) {
		return (ImageButton) view.findViewById(id);
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	public BaseAdapter getAdapter() {
		return this.adapter;
	}

	private int preLinearlayoutConut(int index) {
		int count = 0;
		LinearLayout tempLayout;
		for (int i = index - 1; i >= 0; i--) {
			tempLayout = (LinearLayout) findViewById(RootAdapter.myMemuClassId[i]);
			count = count + tempLayout.getChildCount();
		}
		// System.out.println(count);
		return count;
	}

	private int calcOff(int index) {
		int off = 0;// 移动时X坐标
		int precount = 0;// 前面控件的个数
		int width = getMeasuredWidth() / 5;// 得到屏幕宽度的五分之一
		System.out.println(index);
		if (((LinearLayout) findViewById(RootAdapter.myMemuClassId[index]))
				.getChildCount() == 2)// 当前linearlayout控件个数为2时
		{
			precount = preLinearlayoutConut(index);
			off = (int) ((precount - 1.5) * width);
		} else {// 三个控件以上
			precount = preLinearlayoutConut(index);
			off = (precount - 1) * width;
		}
		return off;
	}

	// 初始化点击事件。
	public void iniOnClickListener() throws ClassNotFoundException {
		// adapter2 = new AlbumAdapter(this.getContext(), new
		// ArrayList<AlbumInfo>());
		for (int i = 0; i < RootAdapter.myMemuClass.length; i++) {
			for (int j = 0; j < ((LinearLayout) findViewById(RootAdapter.myMemuClassId[i]))
					.getChildCount(); j++) {
				final Class<?> myclass = Class
						.forName(RootAdapter.myClass[i][j]);
				// System.out.println(RootAdapter.myClass[i][j]);
				if (i == 0) {
					if (j == 0) {
						((LinearLayout) findViewById(RootAdapter.myMemuClassId[i]))
								.getChildAt(j).setOnClickListener(
										new OnClickListener() {

											@Override
											public void onClick(View v) {
												// System.out.println("123456");
												Intent intent = new Intent();
												if (myclass != null) {
													intent.setClass(context,
															myclass);
													context
															.startActivity(intent);
													System.exit(1);// 防止在第一个Activity重复切换时，退出不程序返回到上一个。
												}

											}
										});
					} else {
						((LinearLayout) findViewById(RootAdapter.myMemuClassId[i]))
								.getChildAt(j).setOnClickListener(
										new OnClickListener() {

											@Override
											public void onClick(View v) {
												// System.out.println("123456");
												Intent intent = new Intent();
												if (myclass != null) {
													AlbumInfo aInfo = new AlbumInfo();
													aInfo
															.setAlbumId(AlbumInfo.CustomALBUMID);
													String pahtString=Environment.getExternalStorageDirectory().getPath()+"/DCIM/Gallery2/";
													File file =new File(pahtString);
													if (file.exists()) {
														aInfo
																.setAlbumItems(Utils
																		.getPictures(pahtString));
														System.out.println("path:"+pahtString+"size:"+aInfo.getAlbumItems().size());
													} else {
														aInfo
																.setAlbumItems(new ArrayList<ImageInfo>());
													}
													System.out.println("path:"+file.exists());
													System.out.println("path:"+pahtString+"size:"+aInfo.getAlbumItems().size());
													Bundle bundle = new Bundle();
													bundle
															.putSerializable(
																	AlbumInfo.SERALIZABLEKEY,
																	aInfo);
													intent.putExtras(bundle);
													intent.setClass(context,
															myclass);
													context
															.startActivity(intent);
												}

											}
										});
					}

				} else {
					((LinearLayout) findViewById(RootAdapter.myMemuClassId[i]))
							.getChildAt(j).setOnClickListener(
									new OnClickListener() {

										@Override
										public void onClick(View v) {
											// System.out.println("123456");
											Intent intent = new Intent();
											if (myclass != null) {
												intent.setClass(context,
														myclass);
												context.startActivity(intent);
											}
										}
									});
				}
			}
		}
	}
}
